<?php
declare (strict_types=1);

namespace app\common\model;

class MakePlace extends Base
{
    protected $name = 'user_make_place';

    public function user()
    {
        return $this->hasOne(User::class, 'id', 'uid')->field('id,code,nickname,mobile');
    }

    public function getExamineTimeAttr($value)
    {
        return $value ? date('Y-m-d H:i:s', $value) : '';
    }

    public function getStatusTextAttr($value, $data)
    {
        switch ($data['status']) {
            case 1:
                return "待审核";
            case 2:
                return "已通过";
            case 3:
                return "已驳回";
            default:
                return "";
        }
    }

    public static function batch($ids, $admin, $type)
    {
        parent::startTrans();
        try {
            $infos = parent::where('status', 1)->whereIn('id', $ids)->select();
            if ($infos->isEmpty()) {
                return ['code' => -1, 'msg' => '未查询到待审核的数据，请检查！'];
            }
            $newIds = [];
            foreach ($infos as $v) {
                $uidAll [] = $v->uid;
                $newIds[] = $v->id;
            }
            parent::update(['status' => $type, 'admin_id' => $admin->id, 'admin_name' => $admin->admin_name, 'examine_time' => time()], ['id' => $newIds]);

            $msg = $type == 2 ? '通过' : '驳回';
            $msg = '批量' . $msg . '分销商申请,标识ID【' . implode(',', $ids) . '】';
            if ($type == 2) {
                foreach ($infos as $v) {
                    if($v['user_id']!=0){
                        $users=User::find($v['user_id']);
                        if($users->isEmpty() && $users->is_place!=1){
                            $data['parent_id']=0;
                            $data['parent_name']='';
                            $data['parent_code']='';
                        }else{
                            $data['parent_id']=$users->id;
                            $data['parent_name']=$users->nickname;
                            $data['parent_code']=$users->code;
                        }
                    }else{
                        $data['parent_id']=0;
                        $data['parent_name']='';
                        $data['parent_code']='';
                    }
                    $data['is_place']=1;
                    $data['place_time']=time();
                    $data['rname']=$v['title'];
                    User::update($data, ['id' => $v['uid']]);
                }
            }
            Logs::adds($msg, $admin['id'], $admin['admin_name']);
            parent::commit();
            return ['code' => 1, 'msg' => '已批量审核'];
        } catch (\Exception $e) {
            parent::rollback();
            return ['code' => -1, 'msg' => $e->getMessage()];
        }
    }

    public static function add($param, $user): array
    {
        try {
            if ($user->is_place) {
                return ['code' => -1, 'msg' => '您已经是分销商了，请勿重复申请'];
            }
            $count = parent::where('uid', $user->id)->where('is_del',0)->where('status', 1)->count();
            if ($count) {
                return ['code' => -1, 'msg' => '您的申请正在审核中，请耐心等待'];
            }
            $param['uid'] = $user->id;
            parent::create($param);
            return ['code' => 1, 'msg' => '申请已提交请耐心等待审核！'];
        } catch (\Exception $e) {
            return ['code' => -1, 'msg' => $e->getMessage()];
        }
    }
}
